<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/9
 * Time: 15:44
 */
namespace App\Http\Controllers\Api\V2;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Modules\EntMemberInfo;
use App\Modules\EntBookSumUp;
use App\Modules\EntBookCollect;
use App\Modules\EntBookViewLog;
use App\Modules\EntMemberDRP;
use App\Modules\EntBookRenewLog;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\File;
use Illuminate\Http\UploadedFile;
use Validator, DB;



class MemberCenterController extends BaseController
{
    protected $url;
    protected $urlapi;
    protected $fileTypes = array('jpg', 'jpeg', 'png');

    public function __construct(){
        $this->url      = env('ADMIN_HOST');
        $this->urlapi   = env('API_HOST');
    }




    /**
     * 读书改进计划
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMemBookSumUp(Request $request)
    {
        $result = 'no';
        $rules = [
            'mobile' => 'required',
            'key'   => 'required'
        ];
        $messages = [
            'mobile.required' => '2037',
            'key.required' => '2038'
        ];
        $mobile = $request->input('mobile');
        $key = $request->input('key');
        $params = ['mobile' => $mobile, 'key' => $key];
        $v = Validator::make($params, $rules, $messages);
        if ($v->fails()) {
            return response()->json(['message' => $result, 'code' => $v->messages()->first()]);
        }
        $member = $this->memNumber($mobile, $key);
        if (count($member) > 0) {
            $entBookSumUp = DB::table('VW_WEB_BookSumUps')
                ->select('BookID', 'BookTitle', 'FilePath1', 'AddDate', 'BookReview1', 'BookReview2', 'BookReview3', 'BookReview4')
                ->where('MemNumber', $member->MemNumber)
                ->get();
            if (count($entBookSumUp) > 0) {
                foreach ($entBookSumUp as $sunup) {
                    $sunup->FilePath1 = $this->url . $sunup->FilePath1;
                }
            }
            return response()->json(['message' => 'ok', 'code' => '200', 'sumuplist' => $entBookSumUp]);
        } else {
            return response()->json(['message' => $result, 'code' => '3027']);
        }
    }

    /**
     * 收藏记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMemBookCollect(Request $request)
    {
        $result = 'no';
        $rules = [
            'mobile' => 'required',
            'key' => 'required'
        ];
        $messages = [
            'mobile.required' => '2037',
            'key.required' => '2038'
        ];
        $mobile = $request->input('mobile');
        $key = $request->input('key');
        $params = ['mobile' => $mobile, 'key' => $key];
        $v = Validator::make($params, $rules, $messages);
        if ($v->fails()) {
            return response()->json(['message' => $result, 'code' => $v->messages()->first()]);
        }
        $member = $this->memNumber($mobile, $key);
        if (count($member) > 0) {
            $entBookCollect = DB::table('bookcollect as a')->leftJoin('bookinfo as b','a.BookID','=','B.BookID')->select('b.BookID','a.AddDate','b.FilePath1')->where('a.MemNumber',$member->MemNumber)->get();

            if (count($entBookCollect) > 0) {
                foreach ($entBookCollect as $collect) {
                    $collect->FilePath1 = $this->url . $collect->FilePath1;
                }
            }

            return response()->json(['message' => 'ok', 'code' => '200', 'collectlist' => $entBookCollect]);
        } else {
            return response()->json(['message' => $result, 'code' => '3027']);
        }
    }

    /**
     * 观看记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMemBookViewLog(Request $request)
    {
        $result = 'no';
        $rules = [
            'mobile' => 'required',
            'key' => 'required'
        ];
        $messages = [
            'mobile.required' => '2037',
            'key.required' => '2038'
        ];
        $mobile = $request->input('mobile');
        $key = $request->input('key');
        $params = ['mobile' => $mobile, 'key' => $key];
        $v = Validator::make($params, $rules, $messages);
        if ($v->fails()) {
            return response()->json(['message' => $result, 'code' => $v->messages()->first()]);
        }
        $member = $this->memNumber($mobile, $key);
        if (count($member) > 0) {
            $entBookViewLog = DB::table('VW_WEB_BookViewLogs')->select('BookID', 'BookTitle', 'FilePath1', 'StartTime')->where('MemNumber', $member->MemNumber)->orderBy('AddDate', 'desc')->get();
            if (count($entBookViewLog) > 0) {

            }
            return response()->json(['message' => 'ok', 'code' => '200', 'viewloglist' => $entBookViewLog]);
        } else {
            return response()->json(['message' => $result, 'code' => '3027']);
        }
    }

    /**
     * 删除收藏记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delMemBookCollect(Request $request)
    {
        $result = 'no';
        $rules = [
            'id' => 'required',
            'mobile' => 'required',
            'key' => 'required',
            'timestamp' => 'required',
            'sign' => 'required'
        ];
        $messages = [
            'id.required' => '2039',
            'mobile.required' => '2037',
            'key.required' => '2038',
            'timestamp.required' => '2040',
            'sign.required' => '2041'
        ];
        $id = $request->input('id');
        $mobile = $request->input('mobile');
        $key = $request->input('key');
        $sign = $request->input('sign');
        $timestamp = $request->input('timestamp');
        $params = ['mobile' => $mobile, 'key' => $key, 'id' => $id, 'timestamp' => $timestamp, 'sign' => $sign];
        $v = Validator::make($params, $rules, $messages);
        if ($v->fails()) {
            return response()->json(['message' => $result, 'code' => $v->messages()->first()]);
        }
        if ($this->checkSign($params) == true) {
            $member = $this->memNumber($mobile, $key);
            if (count($member) > 0) {
                EntBookCollect::where('MemNumber', $member->MemNumber)->where('BookID', $id)->delete();
                return response()->json(['message' => 'ok', 'code' => '200']);
            } else {
                return response()->json(['message' => $result, 'code' => '3027']);
            }
        } else {
            return response()->json(['message' => $result, 'code' => '3028']);
        }
    }

    /**
     * 会员缴费记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMemRenew(Request $request)
    {
        $result = 'no';
        $rules = [
            'mobile' => 'required',
            'key' => 'required'
        ];
        $messages = [
            'mobile.required' => '2037',
            'key.required' => '2038'
        ];
        $mobile = $request->input('mobile');
        $key = $request->input('key');
        $params = ['mobile' => $mobile, 'key' => $key];
        $v = Validator::make($params, $rules, $messages);
        if ($v->fails()) {
            return response()->json(['message' => $result, 'code' => $v->messages()->first()]);
        }
        $member = $this->memNumber($mobile, $key);
        if (count($member) > 0) {
            $entBookRenewLog = EntBookRenewLog::select('ViewLogID', 'RenewDesc', 'AddDate')->where('MemNumber', $member->MemNumber)->get();
            return response()->json(['message' => 'ok', 'code' => '200', 'renewlist' => $entBookRenewLog]);
        } else {
            return response()->json(['message' => $result, 'code' => '3027']);
        }
    }



    /**
     * 会员信息
     * @param string $mobile
     * @param string $key
     * @return mixed
     */
    protected function memNumber($mobile = '', $key = '')
    {
        $entMemberInfo = EntMemberInfo::select('MemEndTime', 'FilePath1', 'YourName', 'MemNumber')->where('DeleteMark', '1')->where('UserName', $mobile)->where('AppKey', $key)->first();
        return $entMemberInfo;
    }
}